package com.bugman.validators;

import com.bugman.managed.BaseBean;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class EmailValidator extends BaseBean implements Validator {

    private ResourceBundle rb = getBundle();

    public void validate(FacesContext context, UIComponent component, Object object)
            throws ValidatorException {
        
        Pattern pattern = Pattern.compile(".{2,}@.{2,}\\.[a-z]{2,}");

        if (object != null) {
            Matcher matcher = pattern.matcher((String) object);
            if (!matcher.matches()) {
                throw new ValidatorException(new FacesMessage(
                        FacesMessage.SEVERITY_ERROR,
                        rb.getString("validatorEmail_error"),
                        rb.getString("validatorEmail_error_detail")));
            }
        }
    }
}
